﻿using Abp.IdentityServer4;
using Abp.Zero.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using ARchGL.Platform.Authorization.Roles;
using ARchGL.Platform.Authorization.Users;
using ARchGL.Platform.Chat;
using ARchGL.Platform.Editions;
using ARchGL.Platform.Friendships;
using ARchGL.Platform.MultiTenancy;
using ARchGL.Platform.MultiTenancy.Accounting;
using ARchGL.Platform.MultiTenancy.Payments;
using ARchGL.Platform.Storage;
using ARchGL.Platform.Core;

namespace ARchGL.Platform.EntityFrameworkCore
{
    public class HostDbContext : AbpZeroHostDbContext<Tenant, Role, User, HostDbContext>, IAbpPersistedGrantDbContext
    {
        /* Define an IDbSet for each entity of the application */

        #region Abp 原有数据实体类

        public virtual DbSet<BinaryObject> BinaryObjects { get; set; }

        public virtual DbSet<Friendship> Friendships { get; set; }

        public virtual DbSet<ChatMessage> ChatMessages { get; set; }

        public virtual DbSet<SubscribableEdition> SubscribableEditions { get; set; }

        public virtual DbSet<SubscriptionPayment> SubscriptionPayments { get; set; }

        public virtual DbSet<Invoice> Invoices { get; set; }

        public virtual DbSet<PersistedGrantEntity> PersistedGrants { get; set; }

        #endregion

        #region 单元测试专用 Huahui 数据实体类
        public virtual DbSet<AppVersions> AppVersions { get; set; }
        public virtual DbSet<OrganizationUnitExtend> OrganizationUnitExtend { get; set; }
        public virtual DbSet<Notices> Notices { get; set; }
        public virtual DbSet<NoticesUsers> NoticesUser { get; set; }
        public virtual DbSet<Projects> Projects { get; set; }
        public virtual DbSet<ProjectUsers> ProjectUsers { get; set; }
        public virtual DbSet<ProjectModels> ProjectModels { get; set; }
        public virtual DbSet<ProjectModelGroups> ProjectModelGroups { get; set; }
        public virtual DbSet<ProjectViewPorts> ProjectViewPorts { get; set; }
        public virtual DbSet<ReportWarehouses> ReportWarehouses { get; set; }
        public virtual DbSet<SelfChecks> SelfChecks { get; set; }
        public virtual DbSet<SecurityChecks> SecurityChecks { get; set; }
        public virtual DbSet<SecurityCheckItems> SecurityCheckItems { get; set; }
        public virtual DbSet<SecurityChecksTurnOverHistorys> SecurityChecksTurnOverHistorys { get; set; }
        public virtual DbSet<SecurityCheckAndFeedbacks> SecurityCheckAndFeedbacks { get; set; }
        public virtual DbSet<Attachments> SecurityCheckAttachments { get; set; }
        public virtual DbSet<ProjectDrawings> ProjectDrawings { get; set; }
        public virtual DbSet<DocumentLibrarys> DocumentLibrarys { get; set; }
        public virtual DbSet<DocumentShares> DocumentShares { get; set; }
        public virtual DbSet<UserLoginAttemptExtend> UserLoginAttemptExtend { get; set; }
        public virtual DbSet<UserRoleExtend> UserRoleExtend { get; set; }
        public virtual DbSet<UserOrganizationUnitExtend> UserOrganizationUnitExtend { get; set; }

        public virtual DbSet<ProjectCategoryTemplate> ProjectCategoryTemplate { get; set; }

        public virtual DbSet<ProjectProperties> ProjectProperties { get; set; }
        #endregion

        #region Qianbian 数据实体

        public virtual DbSet<QB_ProjectAttendances> QB_ProjectAttendances { get; set; }
        public virtual DbSet<QB_ProjectCranes> QB_ProjectCranes { get; set; }
        public virtual DbSet<QB_ProjectDevices> QB_ProjectDevices { get; set; }
        public virtual DbSet<QB_ProjectEnvironments> QB_ProjectEnvironments { get; set; }
        public virtual DbSet<QB_ProjectLifts> QB_ProjectLifts { get; set; }
        public virtual DbSet<QB_Projects> QB_Projects { get; set; }
        public virtual DbSet<QB_ProjectUsers> QB_ProjectUsers { get; set; }
        public virtual DbSet<QB_ProjectVideos> QB_ProjectVideos { get; set; }
        public virtual DbSet<QB_ProjectVideoPlays> QB_ProjectVideoPlays { get; set; }

        public virtual DbSet<QB_EventJobs> QB_EventJobs { get; set; }
        #endregion

        #region ThirdParty

        public virtual DbSet<AttendanceDevices> AttendanceDevices { get; set; }
        public virtual DbSet<EnvironmentReports> EnvironmentReports { get; set; }
        public virtual DbSet<Environments> Environments { get; set; }  
        public virtual DbSet<Hoists> Hoists { get; set; }
        public virtual DbSet<HoistRealDatas> HoistRealDatas { get; set; }
        public virtual DbSet<HoistReports> HoistReports { get; set; }
        public virtual DbSet<Machines> Machines { get; set; }
        public virtual DbSet<TowerCranes> TowerCranes { get; set; }
        public virtual DbSet<TowerCranesRealDatas> TowerCranesRealDatas { get; set; }
        public virtual DbSet<TowerCranesReports> TowerCranesReports { get; set; }
        public virtual DbSet<TP_ProjectUsers> TP_ProjectUsers { get; set; }
        public virtual DbSet<ProjectUsersExtends> ProjectUsersExtend { get; set; }
        public virtual DbSet<ProjectUsersRegister> ProjectUsersRegister { get; set; }
        public virtual DbSet<ProjectUserAttendances> ProjectUserAttendances { get; set; }
        public virtual DbSet<ProjectUserAttendanceBinaryObjects> ProjectUserAttendanceBinaryObjects { get; set; }        
        public virtual DbSet<Teams> Teams { get; set; }
        public virtual DbSet<Majors> Majors { get; set; }

        #endregion

        public HostDbContext(DbContextOptions<HostDbContext> options)
            : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<BinaryObject>(b =>
            {
                b.HasIndex(e => new { e.TenantId });
            });

            modelBuilder.Entity<ChatMessage>(b =>
            {
                b.HasIndex(e => new { e.TenantId, e.UserId, e.ReadState });
                b.HasIndex(e => new { e.TenantId, e.TargetUserId, e.ReadState });
                b.HasIndex(e => new { e.TargetTenantId, e.TargetUserId, e.ReadState });
                b.HasIndex(e => new { e.TargetTenantId, e.UserId, e.ReadState });
            });

            modelBuilder.Entity<Friendship>(b =>
            {
                b.HasIndex(e => new { e.TenantId, e.UserId });
                b.HasIndex(e => new { e.TenantId, e.FriendUserId });
                b.HasIndex(e => new { e.FriendTenantId, e.UserId });
                b.HasIndex(e => new { e.FriendTenantId, e.FriendUserId });
            });

            modelBuilder.Entity<Tenant>(b =>
            {
                b.HasIndex(e => new { e.SubscriptionEndDateUtc });
                b.HasIndex(e => new { e.CreationTime });
            });

            modelBuilder.Entity<SubscriptionPayment>(b =>
            {
                b.HasIndex(e => new { e.Status, e.CreationTime });
                b.HasIndex(e => new { e.PaymentId, e.Gateway });
            });

            modelBuilder.ConfigurePersistedGrantEntity();

            base.OnModelCreating(modelBuilder);
        }
    }

    public class TenantDbContext : AbpZeroTenantDbContext<Role, User, TenantDbContext>
    {
        #region Huahui 数据实体类
        public virtual DbSet<AppVersions> AppVersions { get; set; }
        public virtual DbSet<OrganizationUnitExtend> OrganizationUnitExtend { get; set; }
        public virtual DbSet<Notices> Notices { get; set; }
        public virtual DbSet<NoticesUsers> NoticesUser { get; set; }
        public virtual DbSet<Projects> Projects { get; set; }
        public virtual DbSet<ProjectUsers> ProjectUsers { get; set; }
        public virtual DbSet<ProjectModels> ProjectModels { get; set; }
        public virtual DbSet<ProjectModelGroups> ProjectModelGroups { get; set; }
        public virtual DbSet<ProjectViewPorts> ProjectViewPorts { get; set; }
        public virtual DbSet<ReportWarehouses> ReportWarehouses { get; set; }
        public virtual DbSet<SelfChecks> SelfChecks { get; set; }
        public virtual DbSet<SecurityChecks> SecurityChecks { get; set; }
        public virtual DbSet<SecurityCheckItems> SecurityChecksItem { get; set; }
        public virtual DbSet<SecurityCheckAndFeedbacks> SecurityCheckAndFeedbacks { get; set; }
        public virtual DbSet<SecurityChecksTurnOverHistorys> SecurityChecksTurnOverHistorys { get; set; }
        public virtual DbSet<Attachments> SecurityChecksAttachment { get; set; }
        public virtual DbSet<ProjectDrawings> ProjectDrawings { get; set; }
        public virtual DbSet<DocumentLibrarys> DocumentLibrarys { get; set; }
        public virtual DbSet<DocumentShares> DocumentShares { get; set; }
        public virtual DbSet<UserLoginAttemptExtend> UserLoginAttemptExtend { get; set; }
        public virtual DbSet<UserRoleExtend> UserRoleExtend { get; set; }
        public virtual DbSet<UserOrganizationUnitExtend> UserOrganizationUnitExtend { get; set; }

        public virtual DbSet<ProjectCategoryTemplate> ProjectCategoryTemplate { get; set; }

        public virtual DbSet<ProjectProperties> ProjectProperties { get; set; }

        #endregion

        #region Qianbian 数据实体

        public virtual DbSet<QB_ProjectAttendances> QB_ProjectAttendances { get; set; }
        public virtual DbSet<QB_ProjectCranes> QB_ProjectCranes { get; set; }
        public virtual DbSet<QB_ProjectDevices> QB_ProjectDevices { get; set; }
        public virtual DbSet<QB_ProjectEnvironments> QB_ProjectEnvironments { get; set; }
        public virtual DbSet<QB_ProjectLifts> QB_ProjectLifts { get; set; }
        public virtual DbSet<QB_Projects> QB_Projects { get; set; }
        public virtual DbSet<QB_ProjectUsers> QB_ProjectUsers { get; set; }
        public virtual DbSet<QB_ProjectVideos> QB_ProjectVideos { get; set; }
        public virtual DbSet<QB_ProjectVideoPlays> QB_ProjectVideoPlays { get; set; }

        public virtual DbSet<QB_EventJobs> QB_EventJobs { get; set; }
        #endregion

        #region ThirdParty

        public virtual DbSet<AttendanceDevices> AttendanceDevices { get; set; }
        public virtual DbSet<EnvironmentReports> EnvironmentReports { get; set; }
        public virtual DbSet<Environments> Environments { get; set; }
        public virtual DbSet<Hoists> Hoists { get; set; }
        public virtual DbSet<HoistRealDatas> HoistRealDatas { get; set; }
        public virtual DbSet<HoistReports> HoistReports { get; set; }
        public virtual DbSet<Machines> Machines { get; set; }
        public virtual DbSet<TowerCranes> TowerCranes { get; set; }
        public virtual DbSet<TowerCranesRealDatas> TowerCranesRealDatas { get; set; }
        public virtual DbSet<TowerCranesReports> TowerCranesReports { get; set; }
        public virtual DbSet<TP_ProjectUsers> TP_ProjectUsers { get; set; }
        public virtual DbSet<ProjectUsersExtends> ProjectUsersExtend { get; set; }
        public virtual DbSet<ProjectUsersRegister> ProjectUsersRegister { get; set; }
        public virtual DbSet<ProjectUserAttendances> ProjectUserAttendances { get; set; }
        public virtual DbSet<ProjectUserAttendanceBinaryObjects> ProjectUserAttendanceBinaryObjects { get; set; }
        public virtual DbSet<Teams> Teams { get; set; }
        public virtual DbSet<Majors> Majors { get; set; }

        #endregion

        public TenantDbContext(DbContextOptions<TenantDbContext> options)
            : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 把插件中的实体加到模型中
            this.AddEntityFromPlugins(modelBuilder);
            this.AddEntityFromTypes(modelBuilder, typeof(HostDbContext).Assembly.GetTypes());
            base.OnModelCreating(modelBuilder);
        }
    }

    public class AbpZeroDbContextBase
    {

    }
}
